local UIBase = require("view.UIBase")
local PackageView = class("PackageView", UIBase)
local UIUtil = require("utils.UIUtil")
local UserDataHelper = require("data.helper.UserDataHelper")
local TypeHelper = require("utils.TypeHelper")
local DataConst = require("const.DataConst")

local ITEM_NUM = 5

function PackageView:ctor()
    local resource = {
        path = "Prefabs/View",
        name = "PackageView",
        canvas = 2
    }
    self._selectedIndex = 1
    PackageView.super.ctor(self, resource)
end

function PackageView:start()
    local money = UserDataHelper.getNumByTypeValue(TypeHelper.TYPE_RESOURCE, DataConst.RES_MONEY)
    self._textMoney.text = Lang.get("lang_have_money", {num = money})
    self._btnItems.onClick:AddListener(handler(self, self._onBtnItemsClick))
    self._btnEquip.onClick:AddListener(handler(self, self._onBtnEquipClick))
    self._btnClose.onClick:AddListener(handler(self, self._onBtnCloseClick))
    self._btnHero.onClick:AddListener(handler(self, self._onBtnHeroClick))
    self._btnMagickey.onClick:AddListener(handler(self, self._onMagickeyClick))
    self._listView = self._scrollView
    self._listView:SetTemplate(
        handler(self, self._onItemUpdate),
        handler(self, self._onValueChange),
        handler(self, self._onItemTouch)
    )
    self:_updateView()
end

function PackageView:_updateData()
    if self._selectedIndex == 1 then
        self._dataList = G_UserData:getItems():getItemList()
    elseif self._selectedIndex == 2 then
        self._dataList = G_UserData:getEquipment():getEquipmentList()
    elseif self._selectedIndex == 3 then
        self._dataList = G_UserData:getHero():getHeroList()
    elseif self._selectedIndex == 4 then
        self._dataList = G_UserData:getMagickey():getList()
    end
end

function PackageView:_updateView()
    self:_updateData()
    self._textTitle.text = Lang.get("lang_package_title_" .. self._selectedIndex)
    local count = math.ceil(#self._dataList / ITEM_NUM)
    self._listView:ReSize(count)
    self._emptyTip.gameObject:SetActive(count == 0)
end

function PackageView:_onItemUpdate(item, index)
    local startIndx = index * ITEM_NUM + 1
    local endIndex = startIndx + ITEM_NUM
    local lines = {}
    for i = startIndx, endIndex - 1 do
        local data = self._dataList[i]
        if data then
            table.insert(lines, data)
        end
    end
    item:updateUI(lines)
end

function PackageView:_onValueChange()
end

function PackageView:_onItemTouch(item, index)
end

function PackageView:_onBtnItemsClick()
    self:_onTabSelected(1)
end

function PackageView:_onBtnEquipClick()
    self:_onTabSelected(2)
end

function PackageView:_onBtnHeroClick()
    self:_onTabSelected(3)
end

function PackageView:_onMagickeyClick()
    self:_onTabSelected(4)
end

function PackageView:_onTabSelected(index)
    if self._selectedIndex == index then
        return
    end
    self._selectedIndex = index
    self:_updateView()
end

function PackageView:_onBtnCloseClick()
    self:close()
end

return PackageView
